/**
 * @class common.messagebox
 * @extends common.base.object
 * 对话框
 */
define(function (require) {
    var BaseObject = require('common/base/object'),
        Tpl = require('text!common/templates/messagebox.html');

    var Messagebox = BaseObject.extend({
        /**
         * 构造函数
         * @param  {Object} params 参数
         */
        initialize: function (params) {
            var _this = this;
            this.id = _.time() + "_" + _.rand(1, 10000);
        },        
        warning: function (text,returnHtml) {
            var html=this.render(Tpl, {type: 'warning', text: text, id: this.id});
            return this.show(html,returnHtml);
        },
        success: function (text,returnHtml) {
            var html=this.render(Tpl, {type: 'success', text: text, id: this.id});
            return this.show(html,returnHtml);
        },
        info: function (text,returnHtml) {
            var html=this.render(Tpl, {type: 'info', text: text, id: this.id});
            return this.show(html,returnHtml);
        },
        error: function (text,returnHtml) {
            var html=this.render(Tpl, {type: 'error', text: text, id: this.id});
            return this.show(html,returnHtml);
        },
        show: function (html,returnHtml) {
            if(returnHtml)return html;
            $('body').append('<div class="messagebox" id="messagebox_'+this.id+'">'+html+'</div>');
            var _this = this,
                $el = $('#messagebox_' + this.id);

            $el.css({
                left: ($(window).width() / 2 - $el.width() / 2) + "px"
            });
            $el.show();
            $el.animate({top: '10px'}, 200);
            $('.close', $el).unbind('click').click(function () {
                _this.close();
            });
            setTimeout(function () {
                _this.close();
            }, 3000);
        },
        close: function () {
            var $el = $('#messagebox_' + this.id);
            var _this = this;
            $el.animate({top: '-100px'}, 200, function () {
                $el.remove();
            });
        }
    });

    return Messagebox;
});